const rejectMsg = '主动抛出了异常';
const fakeAjaxTime = 200;
const dataColumn = 5;
const dataRow = 5;

class TableData {
  constructor() {
    this.interVal = fakeAjaxTime;

  }

  // 模拟异步获取数据
  // @current：当前的数据位置
  // @take：获取多少条
  // @colunm：数据列数
  getDataList(
    current, take = dataRow, colunm = dataColumn, interval = this.interVal) {
    const self = this;
    return new Promise((resolve, reject, needreject) => {
      setTimeout(() => {
        if (needreject) {
          reject(rejectMsg);
        }
        resolve(self._fakeData(colunm, take, current));
      }, interval);
    });
  }

  // 伪造一个数据对象
  // @column：列数
  // @row：行数
  _fakeData(column, rowCount, current) {
    const result = {title: [], rows: []};
    for (let col = 0; col < column; col++) {
      result.title.push(`列头${col}`);
    }
    for (let row = current; row < rowCount + current; row++) {
      result.rows.push([]);
      for (let col = 0; col < column; col++) {
        result.rows[row - current].push(`数据-${row}-${col}`);
      }
    }

    if (current === 0) {
      result.rows.unshift(result.title);
    }
    return result;
  }

}

export default new TableData();
